package com.example.park2212.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.adapter.RepairAdapter
import com.example.park2212.databinding.ActivityRegisterBinding
import com.example.park2212.databinding.ActivityRepairBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/repair/RepairActivity")
class RepairActivity : BaseActivity<ActivityRepairBinding,RepairViewModel>() {
    val repairAdapter by lazy { RepairAdapter() }
    var flag = false//flag：升序  true:降序
    var isClick = false
    override fun initView() {

        binding.headRepair.headerAdd.setOnClickListener {

            ARouter.getInstance().build("/app/repair/RepairAddActivity").navigation()
        }

        binding.rvRepair.apply {
            adapter = repairAdapter
            layoutManager = LinearLayoutManager(this@RepairActivity)
        }

        repairAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/repair/RepairDetailActivity")
                .withString("rid",repairAdapter.data[position].id.toString())
                .navigation()
        }



        binding.repairOrder.setOnClickListener {
            if (flag){
                //降序
                repairAdapter.data.sortByDescending {
                    it.repairTime
                }
                binding.repairOrderiv.setImageResource(R.drawable.ic_down)
            }else{
                //升序
                repairAdapter.data.sortBy {
                    it.repairTime
                }
                binding.repairOrderiv.setImageResource(R.drawable.ic_up)
            }
            repairAdapter.notifyDataSetChanged()
            flag = !flag
        }

        binding.repairSv.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (isClick){
                    viewModel.getPairByNo(query!!)
                }else{
                    viewModel.getRepairByUname(query!!)
                }

                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }

        })

        binding.repairSv.setOnCloseListener {
            viewModel.getRepair()
            return@setOnCloseListener true
        }

        //筛选
        binding.repairFilter.setOnClickListener {
            isClick = true
        }

        //删除
        ItemTouchHelper(object :ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT){
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition
                repairAdapter.data.removeAt(position)
                repairAdapter.notifyItemRemoved(position)

                viewModel.deleteRepair(repairAdapter.data[position].id!!)
            }

        }).attachToRecyclerView(binding.rvRepair)



    }

    override fun initData() {
        viewModel.getRepairSuccess.observe(this){
            repairAdapter.data.clear()
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }
        viewModel.getPairByNoSuccess.observe(this){
            repairAdapter.data.clear()
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }

        viewModel.deleteRepairSuccess.observe(this){
            ToastUtils.showLong("删除成功")
        }

    }

    override fun onResume() {
        super.onResume()
        viewModel.getRepair()
    }

}